<?php

class Mcategory extends TST_Model {

    protected $table_category = "category";
    protected $selected_columns = "*";

    function Mcategory() {
        parent::TST_Model();
    }

    public function getFirstSubcategoryOfCategory($category_id, $status = AppContants::ACTIVE_STATUS) {
        $sql = "SELECT c.id, c.name, c.friendly_url
                FROM {$this->table_category} c
                WHERE c.status = ?
                AND   c.parent_id = ?     
                ";
        return $this->query_row($sql, array($status, $category_id));
    }

    public function getCategoryByParentId($parent_id, $status = AppContants::ACTIVE_STATUS) {
        $sql = "SELECT id, parent_id, name, friendly_url , status 
                FROM {$this->table_category}
                WHERE status = ?
                AND   parent_id = ?
                ORDER BY in_order, name";

        return $this->query_array($sql, array($status, $parent_id));
    }

    public function getAllCategory($status = AppContants::ACTIVE_STATUS) {
        $sql = "SELECT id, parent_id, name, friendly_url , status 
                FROM {$this->table_category}
                WHERE status = ?                
                ORDER BY id, name";

        return $this->query_array($sql, array($status));
    }

    /*
     * 
     * 
     * 
     * 
     * 
     */

//for Admin ---------------------------------------------------------------
    public function getCategoryDetailByIdADMIN($id) {
        $sql = "SELECT id, parent_id, name, friendly_url , status, in_order, note
                FROM {$this->table_category}
                WHERE id = ? 
                ";

        return $this->query_row($sql, array($id));
    }

    public function getAllCategoryADMIN() {
        $sql = "SELECT id, parent_id, name, friendly_url , status 
                FROM {$this->table_category}
                WHERE status <> 'delete'                
                ORDER BY id, name";

        return $this->query_array($sql, array());
    }

    public function getCategoryByLevelADMIN($level = NULL, $parent_id = 0) {
        $sql = "SELECT id, parent_id, name, friendly_url
                FROM {$this->table_category}
                WHERE status <> 'delete'
                AND   level = ? 
                AND   parent_id = ? 
                ORDER BY in_order, name";

        return $this->query_array($sql, array($level, $parent_id));
    }

    public function update($id, $data) {
        $where = "id = {$id}";
        $sql = $this->db->update_string($this->table_category, $data, $where);
        return $this->db->query($sql);
    }

    public function addNew($data) {
        $this->db->insert($this->table_category, $data);
        return $this->db->insert_id();
    }

    public function delete($id) {
        $data = array(
            'status' => 'delete'
        );
        $where = "id = {$id}";
        $sql = $this->db->update_string($this->table_category, $data, $where);
        return $this->db->query($sql);
    }

    /*
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     * /
     */

    public function getCategoriesByParentIdAdmin($language, $id, $note = NULL) {
        $sql = "SELECT {$this->selected_columns} 
                FROM {$this->table_categories}
                WHERE status NOT LIKE 'delete'
                AND   parent_id = ? 
                AND   language = ?
                AND   note = ?
                ORDER BY in_order, name";

        return $this->query_array($sql, array($id, $language, $note));
    }

}
